package com.example.safeassistan.hiddendanger.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.base.BaseModel;
import com.example.safeassistan.common.UserData;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.hiddendanger.model.HiddenDangerDetailModel;
import com.example.safeassistan.home.activity.ActivityHiddenDangerType;
import com.example.safeassistan.home.activity.AuthorizeDepartMentPerson;
import com.example.safeassistan.home.activity.AuthorizeDepartment;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.main.MainActivity;
import com.example.safeassistan.utile.JsonUtils;
import com.luck.picture.lib.entity.LocalMedia;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class HiddendangerAssess extends BaseActivity implements View.OnClickListener {
    private TextView tvNoAuthorize,tvAuthorize,tvPass,tvNoPass,tvBig,tvNormal,tvEntrustDepart;
    private LinearLayout llAuthorize,llNoAuthorize,llDepartment,llAuthorizer,llReformMethod,
            llReformAssist,llAssessIntro,llNoAuthorizeMid,llReformDepart,llAcceptDepart,llAcceptPerson,
            llReformPerson,llReformDate;
    private TextView tvSubmit,tvReformDepart,tvAcceptDepart,tvAuthorizer,tvAcceptPerson,tvReformPerson,
            tvReformDate,tvHiddendDangerName;
    private EditText tvState,etEntrustState;


    private static final Integer REQUESTCODE=10000;
    private static final Integer REQUESTCODE2=10002;
    private static final Integer REQUESTCODE1=10001;
    private static final Integer REQUESTCODE3=10003;
    private static final Integer REQUESTCODE4=10004;
    private static final Integer REQUESTCODE5=10005;

    private String status="0";//0 不委托 1 委托
    private String result="0";//评审结果 0 通过 1 不通过
    private String hiddenLevel="3";//隐患等级 1 一般 3重大
    private String rectDepart="",acceptDepart="";//整改部门 验收部门
    private String rectPerson="",acceptPerson="";//整改人员 验收人员
    private String rectTime="";//整改日期
    private String state="";//评审说明
    private String hiddenId="";//隐患id
    private String entrustDepart="";//委托部门
    private String entrustPerson="";//委托人员
    private String hiddenName="";//隐患名称
    private String entrustState="";//委托说明/备注
    private String riskPointId="";//风险点id
    private String reviewProject="";//评审方案


    public static void actionStart(Context context,String hiddenId,String hiddenName,String riskPointId) {
        Intent intent = new Intent(context, HiddendangerAssess.class);
        intent.putExtra("hiddenId",hiddenId);
        intent.putExtra("hiddenName",hiddenName);
        intent.putExtra("riskPointId",riskPointId);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSubmit:
                //提交评审
                submitData();
                break;
            case R.id.tvNoAuthorize:
                //没有委托
                reSetAuthorize();
                status="0";
                tvNoAuthorize.setBackgroundResource(R.drawable.stoke_solid_blue_bg_5);
                tvNoAuthorize.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                llNoAuthorize.setVisibility(View.VISIBLE);
                llAuthorize.setVisibility(View.GONE);
                llAssessIntro.setVisibility(View.VISIBLE);
                llReformAssist.setVisibility(View.VISIBLE);
                llNoAuthorizeMid.setVisibility(View.VISIBLE);
                break;
            case R.id.tvAuthorize:
                //委托
                reSetAuthorize();
                status="1";
                tvAuthorize.setBackgroundResource(R.drawable.stoke_solid_blue_bg_5);
                tvAuthorize.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                llNoAuthorize.setVisibility(View.GONE);
                llAuthorize.setVisibility(View.VISIBLE);
                llAssessIntro.setVisibility(View.GONE);
                llReformAssist.setVisibility(View.GONE);
                llNoAuthorizeMid.setVisibility(View.GONE);
                break;
            case R.id.tvNoPass:
                //不通过
                reSetResult();
                result="1";
                tvNoPass.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                tvNoPass.setBackgroundResource(R.drawable.stoke_solid_blue_bg_5);
                llReformAssist.setVisibility(View.GONE);
                llNoAuthorize.setVisibility(View.GONE);
                break;
            case R.id.tvPass:
                //通过
                reSetResult();
                result="0";
                tvPass.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                tvPass.setBackgroundResource(R.drawable.stoke_solid_blue_bg_5);
                llReformAssist.setVisibility(View.VISIBLE);
                llNoAuthorize.setVisibility(View.VISIBLE);
                break;
            case R.id.llAcceptDepart:
                //验收部门
                Intent intent2=new Intent(HiddendangerAssess.this, AuthorizeDepartment.class);
                intent2.putExtra("flag", REQUESTCODE2);
                startActivityForResult(intent2, REQUESTCODE2);
                break;
            case R.id.llReformDepart:
                //整改部门
                Intent intent1=new Intent(HiddendangerAssess.this, AuthorizeDepartment.class);
                intent1.putExtra("flag", REQUESTCODE1);
                startActivityForResult(intent1, REQUESTCODE1);
                break;
            case R.id.llDepartment:
                //委托部门
                Intent intent=new Intent(HiddendangerAssess.this, AuthorizeDepartment.class);
                intent.putExtra("flag", REQUESTCODE);
                startActivityForResult(intent, REQUESTCODE);
                break;
            case R.id.llReformPerson:
                //整改人
                if (rectDepart.equals("")){
                    Toast.makeText(HiddendangerAssess.this,"请先选择整改部门！！",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent4=new Intent(HiddendangerAssess.this, AuthorizeDepartMentPerson.class);
                    intent4.putExtra("flag", REQUESTCODE4);
                    intent4.putExtra("defectId",rectDepart);
                    startActivityForResult(intent4, REQUESTCODE4);
                }
                break;
            case R.id.llAcceptPerson:
                //验收人
                if (acceptDepart.equals("")){
                    Toast.makeText(HiddendangerAssess.this,"请先选择验收部门！！",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent5=new Intent(HiddendangerAssess.this, AuthorizeDepartMentPerson.class);
                    intent5.putExtra("flag", REQUESTCODE5);
                    intent5.putExtra("defectId",acceptDepart);
                    startActivityForResult(intent5, REQUESTCODE5);
                }
             break;
            case R.id.llAuthorizer:
                //委托人
                if (entrustDepart.equals("")){
                    Toast.makeText(HiddendangerAssess.this,"请先选择委托部门！！",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent3=new Intent(HiddendangerAssess.this, AuthorizeDepartMentPerson.class);
                    intent3.putExtra("flag", REQUESTCODE3);
                    intent3.putExtra("defectId",entrustDepart);
                    startActivityForResult(intent3, REQUESTCODE3);
                }
                break;
            case R.id.tvBig:
                //重大
                reSetLevel();
                hiddenLevel="3";
                tvBig.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                tvBig.setBackgroundResource(R.drawable.stoke_solid_blue_bg_5);
                llReformMethod.setVisibility(View.VISIBLE);
                break;
            case R.id.tvNormal:
                //一般
                reSetLevel();
                hiddenLevel="1";
                tvNormal.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
                tvNormal.setBackgroundResource(R.drawable.stoke_solid_blue_bg_5);
                llReformMethod.setVisibility(View.GONE);
                break;
            case R.id.llReformDate:
                //整改日期
                TimePickerView startTime = new TimePickerBuilder(HiddendangerAssess.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        Calendar calendar=Calendar.getInstance();
                        calendar.setTime(date);
                        SimpleDateFormat sf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                        tvReformDate.setText(sf.format(date));
                        rectTime=sf.format(date);
                    }
                })
                        .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                        .setCancelText("取消")//取消按钮文字
                        .isDialog(true)
                        .setSubmitText("确定")//确认按钮文字
                        .setTitleSize(18)//标题文字大小
                        .setTitleColor(getResources().getColor(R.color.white))
                        .setTitleText("整改日期")
                        .setContentTextSize(17)
                        .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                        .isCyclic(true)//是否循环滚动
                        .setTextColorOut(getResources().getColor(R.color.gray6))
                        .setTextColorCenter(getResources().getColor(R.color.blue_3B5BEA))
                        .setSubmitColor(getResources().getColor(R.color.white))//确定按钮文字颜色
                        .setLabel("年", "月", "日", "时", "分", "秒")//默认设���为年月日时分秒
                        .isCenterLabel(false) //是否只显示中间选中项的 label 文字，false 则每项 item 全部都带有 label。
                        .build();
                startTime.show();
                break;

        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.hiddendanger_assess;
    }

    @Override
    protected void initView() {
        setTitleText("隐患评审");
        hiddenId=getIntent().getStringExtra("hiddenId");
        hiddenName=getIntent().getStringExtra("hiddenName");
        riskPointId=getIntent().getStringExtra("riskPointId");
        tvAuthorize=findViewById(R.id.tvAuthorize);
        tvAuthorize.setOnClickListener(this::onClick);
        tvNoAuthorize=findViewById(R.id.tvNoAuthorize);
        tvNoAuthorize.setOnClickListener(this::onClick);
        llAcceptDepart=findViewById(R.id.llAcceptDepart);
        llAcceptDepart.setOnClickListener(this::onClick);
        tvHiddendDangerName=findViewById(R.id.tvHiddendDangerName);
        tvAuthorizer=findViewById(R.id.tvAuthorizer);
        llNoAuthorize=findViewById(R.id.llNoAuthorize);
        tvAcceptDepart=findViewById(R.id.tvAcceptDepart);
        llAuthorize=findViewById(R.id.llAuthorize);
        llAcceptPerson=findViewById(R.id.llAcceptPerson);
        tvReformPerson=findViewById(R.id.tvReformPerson);
        tvNoPass=findViewById(R.id.tvNoPass);
        llReformDate=findViewById(R.id.llReformDate);
        llReformDate.setOnClickListener(this::onClick);
        llReformPerson=findViewById(R.id.llReformPerson);
        llReformPerson.setOnClickListener(this::onClick);
        tvAcceptPerson=findViewById(R.id.tvAcceptPerson);
        tvSubmit=findViewById(R.id.tvSubmit);
        tvPass=findViewById(R.id.tvPass);
        tvReformDate=findViewById(R.id.tvReformDate);
        tvPass.setOnClickListener(this::onClick);
        llDepartment=findViewById(R.id.llDepartment);
        llAuthorizer=findViewById(R.id.llAuthorizer);
        tvNormal=findViewById(R.id.tvNormal);
        tvBig=findViewById(R.id.tvBig);
        tvReformDepart=findViewById(R.id.tvReformDepart);
        tvState=findViewById(R.id.tvState);
        tvEntrustDepart=findViewById(R.id.tvEntrustDepart);
        llReformDepart=findViewById(R.id.llReformDepart);
        llReformMethod=findViewById(R.id.llReformMethod);
        llReformAssist=findViewById(R.id.llReformAssist);
        llAssessIntro=findViewById(R.id.llAssessIntro);
        llNoAuthorizeMid=findViewById(R.id.llNoAuthorizeMid);
        etEntrustState=findViewById(R.id.etEntrustState);
        llAcceptPerson.setOnClickListener(this::onClick);
        tvNormal.setOnClickListener(this::onClick);
        llReformDepart.setOnClickListener(this::onClick);
        tvSubmit.setOnClickListener(this::onClick);
        tvNoPass.setOnClickListener(this::onClick);
        tvBig.setOnClickListener(this::onClick);
        llAuthorizer.setOnClickListener(this::onClick);
        llDepartment.setOnClickListener(this::onClick);


        tvState.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                state=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etEntrustState.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        tvHiddendDangerName.setText(hiddenName);

        if (UserData.create(HiddendangerAssess.this).get(UserData.ls_yh).equals("1")){
            llReformMethod.setVisibility(View.VISIBLE);
        }else {
            llReformMethod.setVisibility(View.GONE);
        }
    }


    public void submitData(){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("status",status);
            if (status.equals("0")){
                jsonObject.put("hiddenLevel",hiddenLevel);
                jsonObject.put("result",Integer.parseInt(result));
            }
            if (!state.equals("")){
                jsonObject.put("state",state);
            }

            if (!rectTime.equals("")){
                jsonObject.put("rectTime",rectTime);
            }

            jsonObject.put("hiddenId",hiddenId);
            jsonObject.put("riskPointId",riskPointId);

            if (!acceptDepart.equals("")){
                jsonObject.put("acceptDepart",acceptDepart);
            }

            if (!rectPerson.equals("")){
                jsonObject.put("rectPerson",rectPerson);
            }

            if (!rectDepart.equals("")){
                jsonObject.put("rectDepart",rectDepart);
            }

            if (!acceptPerson.equals("")){
                jsonObject.put("acceptPerson",acceptPerson);
            }
            if (!entrustState.equals("")){
                jsonObject.put("entrustState",entrustState);
            }
            if(!entrustDepart.equals("")){
                jsonObject.put("entrustDepart",entrustDepart);
            }
            if (!entrustPerson.equals("")){
                jsonObject.put("entrustPerson",entrustPerson);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new HttpRequest(this).doPost(UrlConstants.dangerreview, "",jsonObject, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), BaseModel.class);
                        if (obj instanceof BaseModel) {
                            BaseModel model=(BaseModel) obj;
                            MainActivity.actionStart(HiddendangerAssess.this,2);
                            finish();
                        }
                    }else {
                        Toast.makeText(HiddendangerAssess.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            if (requestCode==REQUESTCODE){
                //委托部门
                entrustDepart=data.getStringExtra("id");
                tvEntrustDepart.setText(data.getStringExtra("name"));
            }else if(requestCode==REQUESTCODE1){
                //整改部门
                rectDepart=data.getStringExtra("id");
                tvReformDepart.setText(data.getStringExtra("name"));

            }else if(requestCode==REQUESTCODE2){
                //验收部门
                tvAcceptDepart.setText(data.getStringExtra("name"));
                acceptDepart=data.getStringExtra("id");
            }else if(requestCode==REQUESTCODE3){
                //委托人
                entrustPerson=data.getStringExtra("id");
                tvAuthorizer.setText(data.getStringExtra("name"));
            }else  if (requestCode==REQUESTCODE4){
                //整改人
                rectPerson=data.getStringExtra("id");
                tvReformPerson.setText(data.getStringExtra("name"));
            }else if(requestCode==REQUESTCODE5){
                //验收人
                acceptPerson=data.getStringExtra("id");
                tvAcceptPerson.setText(data.getStringExtra("name"));
            }
        }
    }

    public void reSetAuthorize(){
        tvNoAuthorize.setBackgroundResource(R.drawable.stoke_gray_bg_5);
        tvAuthorize.setBackgroundResource(R.drawable.stoke_gray_bg_5);
        tvNoAuthorize.setTextColor(getResources().getColor(R.color.gray9));
        tvAuthorize.setTextColor(getResources().getColor(R.color.gray9));
    }

    public void reSetResult(){
        tvPass.setTextColor(getResources().getColor(R.color.gray9));
        tvNoPass.setTextColor(getResources().getColor(R.color.gray9));
        tvNoPass.setBackgroundResource(R.drawable.stoke_gray_bg_5);
        tvPass.setBackgroundResource(R.drawable.stoke_gray_bg_5);
    }
    public void reSetLevel(){
        tvBig.setTextColor(getResources().getColor(R.color.gray9));
        tvNormal.setTextColor(getResources().getColor(R.color.gray9));
        tvNormal.setBackgroundResource(R.drawable.stoke_gray_bg_5);
        tvBig.setBackgroundResource(R.drawable.stoke_gray_bg_5);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}
